<?php
declare(strict_types=1);

namespace SixShop\Message\Hook;

use SixShop\Core\Attribute\Hook;
use SixShop\Message\Entity\MessageNotificationsEntity;
use SixShop\Message\Enum\NotificationsTypeEnum;

readonly class SendNotificationsHook
{
    public function __construct(private MessageNotificationsEntity $messageNotificationsEntity)
    {
    }

    #[Hook('message.send_notifications')]
    public function sendNotifications(array $params): void
    {
        $msgList = [];
        foreach ($params as $msg) {
            $msgList[] = [
                'user_id' => $msg['user_id'],
                'title' => $msg['title'],
                'content' => $msg['content'],
                'type' => NotificationsTypeEnum::from((int)$msg['type']),
                'template_code' => $msg['template_code'],
                'template_context' => $msg['template_context'],
            ];

        }
        $msgList && $this->messageNotificationsEntity->insertAll($msgList);
    }

    #[Hook('message.send_admin_notifications')]
    public function sendAdminNotifications(array $params): void
    {
        $msgList = [];
        foreach ($params as $msg) {
            $msgList[] = [
                'admin_id' => $msg['admin_id'],
                'title' => $msg['title'],
                'content' => $msg['content'],
                'type' => NotificationsTypeEnum::from((int)$msg['type']),
                'template_code' => $msg['template_code'],
                'template_context' => $msg['template_context'],
            ];
        }
        $msgList && $this->messageNotificationsEntity->insertAll($msgList);
    }
}